package io.bluerain.ldrawersample;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.HashMap;
import java.util.Map;

import io.bluerain.ldrawersample.entities.Menu;
import io.bluerain.ldrawersample.entities.SimpleMenu;

/**
 * Created by foredawn on 15-9-22.
 * 侧滑菜单的基本实现
 */
public class MainActivity extends AppCompatActivity {

    public static Menu[] featureMenus = new Menu[]{
            new Menu(R.mipmap.ic_launcher, "菜单01"),
            new Menu(R.mipmap.ic_launcher, "菜单02"),
            new Menu(R.mipmap.ic_launcher, "菜单03"),
            new Menu(R.mipmap.ic_launcher, "菜单04")
    };
    public static Menu[] finalMenus = new Menu[]{
            new Menu(R.mipmap.ic_launcher, "获取资源"),
            new Menu(R.mipmap.ic_launcher, "检查更新"),
            new Menu(R.mipmap.ic_launcher, "关于"),
    };
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle drawerToggle;
    private ListView listMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        findViews();
        toolbar.setTitle("侧滑菜单实现");
        toolbar.setSubtitle("ToolBar+Drawer");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.ic_launcher);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        drawerToggle.syncState();
        drawerLayout.setDrawerListener(drawerToggle);
        //设置菜单列表
        genMenu();
        listMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ItemFragment fragment = new ItemFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("position", position);
                fragment.setArguments(bundle);
                getFragmentManager().beginTransaction()
                        .replace(R.id.content_view, fragment)
                        .commit();
                //关闭所有抽屉
                drawerLayout.closeDrawers();
            }
        });
    }

    private void findViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.dl_left);
        listMenu = (ListView) findViewById(R.id.list_menu);
    }

    private void genMenu() {
        SimpleAdapter adapter = SimpleMenu.builder()
                .setItems(featureMenus)
                .genAdapter(this, R.layout.menu_item,
                        new int[]{R.id.menu_img, R.id.menu_text});

        listMenu.setAdapter(adapter);

        adapter = SimpleMenu.builder()
                .setItems(finalMenus)
                .genAdapter(this, R.layout.menu_item,
                        new int[]{R.id.menu_img, R.id.menu_text});
        ListView finalMenu = (ListView) findViewById(R.id.list_menu_final);
        finalMenu.setAdapter(adapter);
    }
}
